<template>
  <div class="login_container">
    <div class="login_box">
      <img class="logo" src="../assets/logo.png" alt />
      <div class="demo-input-suffix">
        <el-form :rules="rules" ref="form" :model="form">
          <el-form-item prop="username">
            <el-input v-model="form.username" prefix-icon='el-icon-user'></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input type="password" v-model="form.password" prefix-icon='iconfont icon-3702mima'></el-input>
          </el-form-item>
          <el-form-item class="btn">
            <el-button size="mini" type="primary" @click="submitForm('form')">登录</el-button>
            <el-button size="mini" @click="resetForm('form')">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>
<script>
import { Login_api } from "../api/index";
export default {
  data() {
    return {
      form: {
        username: "admin",
        password: "123456",
      },
      rules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          { min: 3, max: 6, message: "长度在 3 到 5 个字符", trigger: "blur" },
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          {
            min: 6,
            max: 12,
            message: "长度在 6 到 12 个字符",
            trigger: "blur",
          },
        ],
      },
    };
  },
  methods: {
    submitForm(form) {
      this.$refs[form].validate(async (valid) => {
        if (valid) {
          const res = await Login_api(this.form);
           window.sessionStorage.setItem('user',JSON.stringify(res.data.data))
          if (res.data.meta.status == 200) {
            this.$message({
              message: "恭喜你，登录成功",
              type: "success",
            })

            window.sessionStorage.setItem('token',res.data.data.token)
            this.$router.push('/home')
          } else {
            this.$message.error("错了哦,登录失败");
          }
        } else {
          return
        }
      })
    },
    resetForm(form) {
      this.$refs[form].resetFields();
    },
  },
};
</script>
<style scoped lang='less'>
.login_container {
  background-color: #59acff;
  height: 100%;
  position: relative;
  .login_box {
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    width: 450px;
    height: 300px;
    border-radius: 3px;
    background-color: #fff;
    padding: 20px;
    img.logo {
      width: 130px;
      height: 130px;
      border-radius: 50%;
      border: 10px solid #fff;
      position: absolute;
      left: 50%;
      transform: translate(-50%, -50%);
      background-color: #ccc;
      box-shadow: 0 0 10px #ddd;
    }
    .el-form {
      position: absolute;
      bottom: 30px;
      width: 90%;
      .btn {
        text-align: right;
      }
    }
  }
}
</style>